package com.borqs.music.tasks;

import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.http.RequestMethod;
import com.borqs.music.model.SongInfo;
import com.borqs.music.util.MD5Utils;
import com.borqs.music.util.MusicConf;

import java.util.HashMap;
import java.util.Map;

public class UploadLyricTask extends Task {
    private static final String TITLE = "title";
    private static final String SINGER = "singer";
    private static final String DURATION = "duration";
    private static final String CONTENT = "content";
    private static final String ISLOCAL = "islocal";
    private static final String SONGMD5 = "songmd5";
    private static final String LYRICMD5 = "lyricmd5";

    private String mTicket;
    private SongInfo mSong;

    public UploadLyricTask(TaskListener listener, SongInfo song, String ticket) {
        super(listener);
        this.mSong = song;
        this.mTicket = ticket;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        return mSong;
    }

    @Override
    public HttpRequest prepareRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(TITLE, mSong.title);
        params.put(SINGER, mSong.artist);
        params.put(DURATION, String.valueOf(mSong.duration));
        params.put(CONTENT, mSong.lyrics);
        params.put(ISLOCAL, mSong.isLocalLyric ? "1" : "0");
        params.put(SONGMD5, mSong.fileMd5);
        params.put(LYRICMD5, MD5Utils.getMD5(mSong.lyrics.getBytes()));

        String URL = MusicConf.getUrl(MusicConf.ReqType.MUSIC_UPLOAD_LYRIC);
        HttpRequest request = new HttpRequest(RequestMethod.POST, URL, mTicket, params);
        return request;
    }

}
